﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using lazada.Models;
using lazada.Repository;
using lazada.ViewModels;

namespace lazada.Controllers
{
    public class AccountController : Controller
    {
        
        public ActionResult Index()
        {
            Account account = checkCookies();
            if (account == null)
                return View();
            else
            {
                AccountModel accModel = new AccountModel();
                if (accModel.login(account.Username,account.Password))
                {
                    Session["username"] = account.Username;
                    return View("Index");
                }
                else
                {
                    ViewBag.Error = "Đăng nhập thất bại, hãy kiểm tra lại Username & Password!";
                    return View(); 
                }

            }

        }
        
            

         public ActionResult Logout()
        {
            Session.Remove("username");
            if (Response.Cookies["username"] != null)
            {
                HttpCookie ckUsername = new HttpCookie("username");
                ckUsername.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(ckUsername);
            }
            if (Response.Cookies["password"] != null)
            {
                HttpCookie ckPassword = new HttpCookie("password");
                ckPassword.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(ckPassword);
            }
            return RedirectToAction("Index","Home");
        }
         public ActionResult Trangchu()
         {
             
             return RedirectToRoute("Index", "Home");
         }
        [HttpPost]
         public ActionResult Login(AccountViewModel avm)
        {
            AccountModel accModel = new AccountModel();
            if(accModel.login(avm.account.Username,avm.account.Password))
            {
                Session["username"] = avm.account.Username;
                if(avm.remember)
                {
                    HttpCookie ckUsername = new HttpCookie("username");
                    ckUsername.Expires = DateTime.Now.AddSeconds(3600);
                    ckUsername.Value = avm.account.Username;
                    Response.Cookies.Add(ckUsername);

                    HttpCookie ckPassword = new HttpCookie("password");
                    ckPassword.Expires = DateTime.Now.AddSeconds(3600);
                    ckPassword.Value = avm.account.Password;
                    Response.Cookies.Add(ckPassword);
                }
                return View("Welcome");
            }
            else
            {
                ViewBag.Error = "Đăng nhập thất bại, hãy kiểm tra lại Username & Password!";
                return View("Index");
            }
        }
        

        public Account checkCookies()
        {
            Account account = null;
            string username = string.Empty, password = string.Empty;
            if (Request.Cookies["username"] != null)
                username = Request.Cookies["username"].Value;
            if (Request.Cookies["password"] != null)
                password = Request.Cookies["password"].Value;
            if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
                account = new Account { Username = username, Password = password };

            return account;
            
        }
	}
}